<?php

$stime = microtime(true);

include "ip.php";
include "useragent.php";

$useragent = "";
$content = "";
$num =0;
$format=$_REQUEST["format"];

for($i=0;$i<3;$i++)
{
	$num++;
	$ip = $ips[rand(0,(count($ips)-1))];
	$useragent = $useragents[rand(0,(count($useragents)-1))];
	$result = getHtml($_REQUEST["term"],$ip,$useragent,"");
	$content = $result["body"];
	if($content != "" && !strpos($content,"Not Found"))
	{
		break;
	}
	else
	{
		@file_put_contents("result.txt",$ip."\r\n",FILE_APPEND);
	}
}
$result = array();
if(strpos($content,"PMID ".$_REQUEST["term"]." is not available")) {
    $result["type"] = 0;
}else{
    $result["type"] = 1;
    $html = str_replace("\r","",$content);
    $html = str_replace("\n","",$content);
    $html = preg_replace ( "/\s(?=\s)/","\\1", $content);



    $result["num"] = 1;
    $result["term"] = $_REQUEST["term"];
    preg_match("/<input type=\"hidden\" name=\"csrfmiddlewaretoken\" value=\"(.+?)\">/",$html,$match);
    if($match) $result["token"] = trim($match[1]);
    else $result["token"]="";
    //右侧锚点
    preg_match("/<ul class=\"items-list\">(.+?)<\\/ul>/is",$html,$itemsList);
    if($itemsList){
        preg_match_all("/<li .+?>(.+?)<\\/li>/is",$itemsList[1],$itemsHtml);
        if($itemsHtml){
            $items=array();
            for($j=0;$j<count($itemsHtml[1]);$j++){
                preg_match("/<a href=\"#(.+?)\".+?>(.+?)<\\/a>/",$itemsHtml[1][$j],$itemHtml);
                $item=array();
                if($itemsHtml){
                    $item["name"]=trim($itemHtml[2]);
                    $item["href"]=$itemHtml[1];
                }
                $items[$j]=$item;
            }
            $result["items"] = $items;
        }
    }
    preg_match("/<h1 class=\"heading-title\">(.+?)<\\/h1>/is",$html,$match);
    if($match) $result["title"] = trim($match[1]);
    else $result["title"]="";

    preg_match("/<strong class=\"current-id\" title=\"PubMed ID\">(.+?)<\\/strong>/",$html,$match);
    if($match) $result["pmid"] = $match[1];
    else $result["pmid"]="";

    preg_match("/<div class=\"publication-type\" >(.+?)<\\/div>/",$html,$match);
    if($match) $result["ptype"]=$match[1];
    else $result["ptype"]="";

    preg_match("/<div class=\"journal-actions dropdown-block\"><button .+?> (.+?) <\\/button>/",$html,$match);
    if($match) $result["jour"] = $match[1];
    else $result["jour"]="";


    //2020年5月27日13:22:30 需要添加期刊的详细信息 如果有的话 开始
    //<span class="cit">   <span class="citation-doi"> <span class="secondary-date"> 三处拼接一起
    $jourTemo = "";
    preg_match("/<span class=\"cit\">(.+?)<\\/span>/",$html,$match);

    if($match) $jourTemo = $jourTemo.$match[1];

    preg_match("/<span class=\"citation-doi\">(.+?)<\\/span>/",$html,$match);
    if($match) $jourTemo =$jourTemo.$match[1];

    preg_match("/<span class=\"secondary-date\">(.+?)<\\/span>/",$html,$match);
    if($match) $jourTemo = $jourTemo.$match[1];

    if ($jourTemo!=""){
        $result["jourdetail"] =$jourTemo;
    }else{
        $result["jourdetail"] = "";
    }
    //  //2020年5月27日13:22:30 需要添加期刊的详细信息 如果有的话 结束

    // 2023年11月9日16:03:14 添加是不是撤稿文章
    preg_match("/<h3 class=\"usa-alert-heading\">Retracted article<\\/h3>/",$html,$match);
    if($match) $result["Retracted"] ="1";
    else $result["Retracted"]="0";

    preg_match("/<span class=\"volume-issue-pages\"> (.+?) <\\/span>/",$html,$match);
    if($match) $result["vip"] = $match[1];
    else $result["vip"]="";

    preg_match("/<span class=\"publication-date\" >(.+?)<\\/span>/",$html,$match);
    if($match) $result["year"] =preg_replace("/<.+?>/is","", $match[1]);
    else $result["year"]="";

    preg_match("/<div class=\"authors-list\">(.+?)<\\/div>/",$html,$match);
    if($match){
        $aushtml = preg_replace("/<span class=\"author-sup-separator\">.+?<\\/span>/","",$match[1]);
        preg_match_all("/<span class=\"authors-list-item \">(.+?)<\\/span>/",$aushtml,$matchess);
        $aus = array();
        for($j = 0; $j < count($matchess[1]); $j++) {
            $auhtml = $matchess[1][$j];
            preg_match("/<a class=\"full-name\" href=.+?erm=(.+?)&amp;cauthor_id.+?data-ga-action=\"author_link\".+?>(.+?)<\\/a>/",$auhtml,$matchau);
            if($matchau){
                $au["nameiso"] = str_replace("+", " ", $matchau[1]);
                $au["name"] = $matchau[2];
                // 下面这个需要情况 否则就会出现上个的带过来
                $au["num"] = array();
                preg_match_all("/<a class=\"affiliation-link\".+?>(.+?)<\\/a>/", $auhtml, $matchaunum);
                if ($matchaunum) {
                    for ($a = 0; $a < count($matchaunum[1]); $a++) {
                        $au["num"][$a] = $matchaunum[1][$a];
                    }
                } else {
                    $au["num"] = array();
                }
                $aus[$j] = $au;
            }
        }
        $result["author"] = $aus;
    }else{
        $result["author"] = array();
    }
/*    preg_match_all("/<li data-affiliation-id=\"affiliation.+?>(.+?)<\\/li>/",$html,$match);*/
    preg_match_all("/<li data-affiliation-id=\"full-view-affiliation.+?>(.+?)<\\/li>/",$html,$match);
    if($match){
        $affs = array();
//        for($j = 0; $j < count($match[1])/2; $j++) {
        //下面为什么除以2 现在删除
        for($j = 0; $j < count($match[1]); $j++) {
            $affhtml = $match[1][$j];
            preg_match("/<sup class=\"key\">(.+?)<\\/sup> (.+)/",$affhtml,$matchaf);
            if($matchaf){
                $af["num"]=$matchaf[1];
                $af["name"]=$matchaf[2];
                $affs[$j]=$af;
            }
        }
        $result["affs"] = $affs;
    }else{
        $result["affs"] = array();
    }

    preg_match("/<span class=\"id-label\"> PMCID: <\\/span> <a .+?> (.+?) <\\/a>/",$html,$match);
    if($match) $result["pmcid"]=$match[1];
    else $result["pmcid"]="";

    preg_match("/<span class=\"id-label\"> DOI: <\\/span> <a .+?> (.+?) <\\/a>/",$html,$match);
    if($match) $result["doi"]=$match[1];
    else $result["doi"]="";

    preg_match("/id=\"eng-abstract\">(.+?)<\\/div>/",$html,$match);
    if($match) $result["abstr"]=trim($match[1]);
    else $result["abstr"]="";

    preg_match("/<p> <strong class=\"sub-title\"> Keywords: <\\/strong>.+?<\\/p>/",$html,$match);
    if($match) $result["keyword"]=$match[0];
    else $result["keyword"]="";

    preg_match("/id=\"copyright\"> (.+?) <\\/p>/",$html,$match);
    if($match) $result["copyright"]=$match[1];
    else $result["copyright"]="";

    //Conflict of interest statement
    preg_match("/<div class=\"statement\"> <p.+?>(.+?)<\\/p> <\\/div>/",$html,$match);
    if($match) $result["statement"]=$match[1];
    else $result["statement"]="";

    //full-text-links-list
    preg_match("/<div class=\"full-text-links-list\">(.+?)<\\/div>/is",$html,$matchfull);
    if($matchfull){
        $fulltext=array();
        preg_match_all("/<a class=\"link-item(.+?)<\\/a>/",$matchfull[1],$fulls);
        if($fulls){
            for($j=0;$j<count($fulls[0]);$j++){
                $fullt=array();
                preg_match("/<a class=\"link-item.+?href=\"(.+?)\"/",$fulls[0][$j],$full1);
                if($full1){
                    $fullt["href"]=$full1[1];
                }else{
                    $fullt["href"]="";
                }
                preg_match("/<img src=\"(.+?)\"/",$fulls[0][$j],$full2);
                if($full2){
                    $fullt["img"]=$full2[1];
                }else{
                    $fullt["img"]="";
                }

                preg_match("/<span class=\"text\"> (.+?) <\\/span>/",$fulls[0][$j],$full3);
                if($full3){
                    $fullt["name"]=$full3[1];
                    if($fullt["name"]=="Free PMC article"){
                        $fullt["img"]="../img/pmcImg.png";
                    }
                }else{
                    $fullt["name"]="";
                }
                $fulltext[$j]=$fullt;
            }
        }
        $result["fulltext"]=$fulltext;
    }


    //Figures
    preg_match_all("/<figure class=\"figure-item .+?>(.+?)<\\/figure>/",$html,$figuress);
    if($figuress){
        $figures=array();
        for($j=0;$j<count($figuress[1]);$j++){
            $figure=array();
            preg_match("/<div class=\"caption-wrap\"> <strong class=\"figure-label\"> (.+?) <\\/strong>(.+?)<\\/div>/",$figuress[1][$j],$matc2);
            if($matc2){
                $figure["title"]=$matc2[1].preg_replace("/<.+?>/is","", $matc2[2]);
            }
            preg_match("/itemprop=\"description\"> (.+?) <\\/figcaption>/",$figuress[1][$j],$matc3);
            if($matc3){
                $figure["contents"]=$matc3[1];
            }
            preg_match("/<a class=\"figure-link\" href=\"(.+?)\" .+? data-pmc-id=\"(.+?)\" data-figure-id=\"(.+?)\"> <img .+? src=\"(.+?)\".+?\\/>/",$figuress[1][$j],$matc4);
            if($matc4){
                $figure["gif"]=$matc4[4];
                $figure["jpg"]=$matc4[1];
                $figure["href"]="https://www.ncbi.nlm.nih.gov/pmc/articles/".$matc4[2]."/figure/".$matc4[3]."/";
            }

            $figures[$j]=$figure;
        }
        $result["figures"]=$figures;
    }


    //Update of
    preg_match("/<div class=\"linked-articles\" id=\"linked-updated-article\">(.+?)<\\/ul>/",$html,$updateOf);
    if($updateOf){
        preg_match_all("/<li class=\"labs-full-docsum\">(.+?)<\\/li>/",$updateOf[1],$updates);
        if($updates){
            $update=array();
            for($j=0;$j<count($updates[1]);$j++){
                $up=array();
                preg_match("/<a class=\"labs-docsum-title\" href=\"(.+?)\".+?> (.+?) <\\/a>/",$updates[1][$j],$up1);
                if($up1){
                    $up["pmid"]=str_replace("/","",$up1[1]);
                    $up["title"]=$up1[2];
                }
                preg_match("/<div class=\"labs-docsum-citation\"> (.+?) <\\/div>/",$updates[1][$j],$up2);
                if($up2){
                    $up["source"]=preg_replace("/<.+?>/is","",$up2[1]);
                }

                preg_match("/<div class=\"labs-docsum-snippet\"> (.+?) <\\/div>/",$updates[1][$j],$up3);
                if($up3){
                    $up["intro"]=$up3[1];
                }
                $update[$j]=$up;
            }
            $result["updateOf"]=$update;
        }
    }



    //Similar articles 原始备份
//    preg_match("/<div class=\"similar-articles\" id=\"similar\">(.+?)<\\/ul>/",$html,$mSimilar);
//    if($mSimilar){
//        preg_match_all("/<li class=\"labs-full-docsum\">(.+?)<\\/li>/",$mSimilar[1],$similars);
//        if($similars){
//            $similar=array();
//            for($j=0;$j<count($similars[1]);$j++){
//                $sim=array();
/*                preg_match("/<a class=\"labs-docsum-title\" href=\"(.+?)\".+?> (.+?) <\\/a>/",$similars[1][$j],$match1);*/
//                if($match1){
//                    $sim["pmid"]=str_replace("/","",$match1[1]);
//                    $sim["title"]=$match1[2];
//                }
//                preg_match("/<div class=\"labs-docsum-citation\"> (.+?) <\\/div>/",$similars[1][$j],$match2);
//                if($match2){
/*                    $sim["source"]=preg_replace("/<.+?>/is","",$match2[1]);*/
//                }
//
//                preg_match("/<div class=\"labs-docsum-snippet\"> (.+?) <\\/div>/",$similars[1][$j],$match3);
//                if($match3){
//                    $sim["intro"]=$match3[1];
//                }
//                $similar[$j]=$sim;
//            }
//            $result["similar"]=$similar;
//        }
//    }

    //todo 葛成瑞 测试更改similar 2020年7月8日11:05:05
    preg_match("/<div class=\"similar-articles\" id=\"similar\">(.+?)<div class=\"actions-bar\">/is",$html,$mSimilar);
    if($mSimilar){
        preg_match_all("/<li class=\"full-docsum\">(.+?)<\\/li>/is",$mSimilar[1],$similars);
        if($similars){
            $similar=array();
            for($j=0;$j<count($similars[1]);$j++){
                $sim=array();
                preg_match("/<a class=\"docsum-title\" href=\"(.+?)\".+?>(.+?)<\\/a>/is",$similars[1][$j],$match1);
                if($match1){
                    $sim["pmid"]=str_replace("/","",$match1[1]);
                    $sim["title"]=$match1[2];
                }
                preg_match("/<div class=\"docsum-citation\">(.+?)<\\/div>/is",$similars[1][$j],$match2);
                if($match2){
                    $sim["source"]=preg_replace("/<.+?>/is","",$match2[1]);
                }

                preg_match("/<div class=\"docsum-snippet\">(.+?)<\\/div>/is",$similars[1][$j],$match3);
                if($match3){
                    $sim["intro"]=$match3[1];
                }
                $similar[$j]=$sim;
            }
            $result["similar"]=$similar;
        }
    }


    //Cited by
    preg_match("/<div class=\"citedby-articles\" id=\"citedby\">(.+?)<\\/ul>/is",$html,$mcitedby);
    if($mcitedby){
        preg_match_all("/<li class=\"full-docsum\">(.+?)<\\/li>/is",$mcitedby[1],$citedbys);
        if($citedbys){
            $citedby=array();
            for($j=0;$j<count($citedbys[1]);$j++){
                $sim=array();
                preg_match("/<a class=\"docsum-title\" href=\"(.+?)\".+?> (.+?) <\\/a>/is",$citedbys[1][$j],$match1);
                if($match1){
                    $sim["pmid"]=str_replace("/","",$match1[1]);
                    $sim["title"]=$match1[2];
                }
                preg_match("/<div class=\"docsum-citation\"> (.+?) <\\/div>/is",$citedbys[1][$j],$match2);
                if($match2){
                    $sim["source"]=preg_replace("/<.+?>/is","",$match2[1]);
                }

                preg_match("/<div class=\"docsum-snippet\"> (.+?) <\\/div>/is",$citedbys[1][$j],$match3);
                if($match3){
                    $sim["intro"]=$match3[1];
                }
                $citedby[$j]=$sim;
            }
            $result["cited"]=$citedby;
        }
    }

    //References
    preg_match_all("/<div class=\"refs-list\">(.+?)<\\/div>/is",$html,$refHtml);
    if($refHtml){
        $refss=array();
        for($r=0;$r<count($refHtml[1]);$r++){
            $refs=array();
            preg_match("/<h4 class=\"refs-list-title\">(.+?)<\\/h4>/",$refHtml[1][$r],$refTitle);
            if($refTitle){
                $refs["listTitle"]=$refTitle[1];
            }else{
                $refs["listTitle"]="";
            }
            preg_match_all("/<ol class=\"references-and-notes-list\">(.+?)<\\/ol>/",$refHtml[1][$r],$references);
            if($references){
                $refList=array();
                for($j=0;$j<count($references[1]);$j++){
                    $ref=array();
                    if(strpos($references[1][$j],"reference-link")){
                        preg_match("/<li .+?> (.+?) -/",$references[1][$j],$ref2);
                        if($ref2){
                            $ref["title"]=trim($ref2[1]);
                            preg_match_all("/<a class=\"reference-link\" href=\"(.+?)\".+?>(.+?)<\\/a>/",$references[1][$j],$ref3);
                            if($ref3){
                                for($n=0;$n<count($ref3[1]);$n++){
                                    $href["title"]=preg_replace("/<.+?>/is","",trim($ref3[2][$n]));
                                    $href["url"]=$ref3[1][$n];
                                    $ref["href"][$n]=$href;
                                }
                            }
                        }
                    }else{
                        preg_match("/<li class=\"skip-numbering\" value=\"1\"> (.+?) <\\/li>/",$references[1][$j],$ref1);
                        if($ref1){
                            $ref["title"]=preg_replace("/<.+?>/is","",trim($ref1[1]));
                            $ref["pmid"]="";
                        }
                    }
                    $refList[$j]=$ref;
                }
                $refs["list"]=$refList;
            }
            $refss[$r]=$refs;
        }
        $result["refs"]=$refss;
    }

    //Publication types  MeSH terms  Substances  Associated data  Grant support

    //Publication types
    preg_match("/<h2 class=\"title\"> Publication types <\\/h2>(.+?)<\\/ul> <\\/div>/",$html,$matchpt);
    if($matchpt){
        $pts=array();
        preg_match_all("/<li>(.+?)<\\/div><\\/li>/",$matchpt[1],$pubsHtml);
        if($pubsHtml){
            for($j=0;$j<count($pubsHtml[1]);$j++){
                $pt=array();
                preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/",$pubsHtml[1][$j],$ptss1);
                if($ptss1){
                    $pt["type"]=2;
                    $pt["title"]=trim($ptss1[1]);
                    preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/",$ptss1[2],$ptss2);
                    if($ptss2){
                        for($m=0;$m<count($ptss2[1]);$m++){
                            $pt["hrefs"][$m]["name"]=trim($ptss2[2][$m]);
                            $pt["hrefs"][$m]["href"]=$ptss2[1][$m];
                        }
                    }
                    preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/",$ptss1[2],$ptss3);
                    if($ptss3){
                        $pt["hrefs"][count($ptss2[1])]["name"]=$ptss3[2];
                        $pt["hrefs"][count($ptss2[1])]["href"]=str_replace("&quot;","",$ptss3[1]);
                    }
                }
                $pts[$j]=$pt;
            }
        }
        $result["pts"]=$pts;
    }else{
        $result["pts"]=array();
    }

    //MeSH terms
    preg_match("/<h2 class=\"title\"> MeSH terms <\\/h2>(.+?)<\\/ul> <\\/div>/",$html,$matchMesh);
    if($matchMesh){
        $kws=array();
        preg_match_all("/<li>(.+?)<\\/div><\\/li>/",$matchMesh[1],$meshHtml);
        if($meshHtml){
            for($j=0;$j<count($meshHtml[1]);$j++){
                $kw=array();
                preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/",$meshHtml[1][$j],$kwss1);
                if($kwss1){
                    $kw["type"]=2;
                    if(strpos($kwss1[1],"asterisk")){
                        preg_match("/(.+?) <span class=\"asterisk\">/",$kwss1[1],$matchkw);
                        if($matchkw){
                            $kw["title"]=trim($matchkw[1])."*";
                        }
                    }else{
                        $kw["title"]=trim($kwss1[1]);
                    }

                    preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/",$kwss1[2],$kwss2);
                    if($kwss2){
                        for($m=0;$m<count($kwss2[1]);$m++){
                            $kw["hrefs"][$m]["name"]=trim($kwss2[2][$m]);
                            $kw["hrefs"][$m]["href"]=$kwss2[1][$m];
                        }
                    }
                    preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/",$kwss1[2],$kwss3);
                    if($kwss3){
                        $kw["hrefs"][count($kwss2[1])]["name"]=$kwss3[2];
                        $kw["hrefs"][count($kwss2[1])]["href"]=str_replace("&quot;","",$kwss3[1]);
                    }
                }
                $kws[$j]=$kw;
            }
        }
        $result["meshs"]=$kws;
    }else{
        $result["meshs"]=array();
    }
    //Substances
    preg_match("/<h2 class=\"title\"> Substances <\\/h2>(.+?)<\\/ul> <\\/div>/",$html,$meshSub);
    if($meshSub){
        $subs=array();
        preg_match_all("/<li>(.+?)<\\/div><\\/li>/",$meshSub[1],$subHtml);
        if($subHtml) {
            for ($j = 0; $j < count($subHtml[1]); $j++) {
                $sub = array();
                preg_match("/<button class=\"keyword-actions-trigger trigger keyword-link.+?>(.+?)<\\/button>.+?<ul class=\"keyword-actions-links\">(.+?)<\\/ul>/", $subHtml[1][$j], $subss1);
                if ($subss1) {
                    $sub["type"] = 2;
                    $sub["title"] = trim($subss1[1]);
                    preg_match_all("/<a .+? href=\"(.+?)\".+?>(.+?)<\\/a>/", $subss1[2], $subss2);
                    if ($subss2) {
                        for ($m = 0; $m < count($subss2[1]); $m++) {
                            $sub["hrefs"][$m]["name"] = trim($subss2[2][$m]);
                            $sub["hrefs"][$m]["href"] = $subss2[1][$m];
                        }
                    }
                    preg_match("/<a class=\"add-to-search-link .+? data-search-term=\"(.+?)\" .+?> (.+?) <\\/a>/", $subss1[2], $subss3);
                    if ($subss3) {
                        $sub["hrefs"][count($subss2[1])]["name"] = $subss3[2];
                        $sub["hrefs"][count($subss2[1])]["href"] = str_replace("&quot;", "", $subss3[1]);
                    }
                }
                $subs[$j] = $sub;
            }
        }
        $result["subs"]=$subs;
    }else{
        $result["subs"]=array();
    }

    //Associated data
    preg_match("/<h3 class=\"title\"> Associated data <\\/h3>(.+?)<\\/ul> <\\/div>/",$html,$associated);
    if($associated){
        $assData=array();
        if(strpos($associated[1],"<div class=\"title\">Actions</div>")){
            preg_match_all("/<li>(.+?)<\\/div><\\/li>/",$associated[1],$assDatas);
            if($assDatas){
                for($j=0;$j<count($assDatas[1]);$j++){
                    $assd=array();
                    preg_match("/aria-describedby=\"supplemental-data.+?>(.+?)<\\/button>.+?<ul class=\"supplemental-data-actions-links\">(.+?)<\\/ul>/",$assDatas[1][$j],$ass1);
                    if($ass1){
                        $assd["type"]=2;
                        $assd["title"]=trim($ass1[1]);
                        preg_match_all("/<a .+? title=\"(.+?)\" href=\"(.+?)\".+?>/",$ass1[2],$ass2);
                        if($ass2){
                            for($m=0;$m<count($ass2[1]);$m++){
                                $assd["hrefs"][$m]["name"]=$ass2[1][$m];
                                $assd["hrefs"][$m]["href"]=$ass2[2][$m];
                            }
                        }
                    }
                    $assData[$j]=$assd;
                }
            }
        }else{
            preg_match_all("/<a .+? href=\"(.+?)\" .+?>(.+?)<\\/a>/",$associated[1],$assDatas2);
            if($assDatas2){
                for($n=0;$n<count($assDatas2[1]);$n++){
                    $assData[$n]["type"]=1;
                    $assData[$n]["title"]=trim($assDatas2[2][$n]);
                    $assData[$n]["href"]=$assDatas2[1][$n];
                }
            }
        }
        $result["assData"]=$assData;
    }else{
        $result["assData"]=array();
    }



    //Grant support

    preg_match("/<h3 class=\"title\"> Grant support <\\/h3>(.+?)<\\/ul><\\/div>/",$html,$supports);
    if($supports){
        $grants=array();
        if(strpos($supports[1],"<div class=\"title\">Actions</div>")){
        }else{
            preg_match_all("/<a title=\"All articles for grant .+? href=\"(.+?)\" .+?> (.+?) <\\/a>/",$supports[1],$support);
            if($support){
                for($n=0;$n<count($support[1]);$n++){
                    $grants[$n]["type"]=1;
                    $grants[$n]["title"]=trim($support[2][$n]);
                    $grants[$n]["href"]=$support[1][$n];
                }
            }
        }
        $result["grants"]=$grants;
    }else{
        $result["grants"]=array();
    }
    //Grant support
    preg_match("/<div class=\"funding-statement\">(.+?)<\\/div>/",$html,$funding);
    if($funding){
        $grants=array();
        $grants[0]["type"]=0;
        $grants[0]["title"]=$funding[1];
        $result["grants"]=$grants;
    }


    //LinkOut - more resources
//    preg_match_all("/<li><h3 class=\"linkout-category\">.+?<\\/ul><\\/li>/",$html,$mlinks);
//    if($mlinks){
//        $links=array();
//        for($j=0;$j<count($mlinks[0]);$j++){
//            preg_match("/<h3 class=\"linkout-category\">(.+?)<\\/h3>/",$mlinks[0][$j],$linkKey);
//            if($linkKey){
//                $ll=array();
/*                preg_match_all("/<a title=\".+?\" href=\"(.+?)\" .+?> (.+?) <\\/a>/",$mlinks[0][$j],$link1);*/
//                if($link1){
//                    for($m=0;$m<count($link1[1]);$m++){
//                        $link=array();
//                        $link["title"]=$link1[2][$m];
//                        $link["url"]=$link1[1][$m];
//                        $ll[$m]=$link;
//                    }
//                }
//                $links[$linkKey[1]]=$ll;
//            }
//        }
//        $result["linkout"]=$links;
//    }

    //2020年7月19日13:36:48测试
    preg_match_all("/<li><h3 class=\"linkout-category\">.+?<\\/ul><\\/li>/",$html,$mlinks);
    if($mlinks){
        $links=array();
        for($j=0;$j<count($mlinks[0]);$j++){
            preg_match("/<h3 class=\"linkout-category\">(.+?)<\\/h3>/",$mlinks[0][$j],$linkKey);
            if($linkKey){
                $ll=array();
                preg_match_all("/<a href=\"(.+?)\" .+?> (.+?) <\\/a>/",$mlinks[0][$j],$link1);
                if($link1){
                    for($m=0;$m<count($link1[1]);$m++){
                        $link=array();
                        $link["title"]=$link1[2][$m];
                        $link["url"]=$link1[1][$m];
                        $ll[$m]=$link;
                    }
                }
                $links[$linkKey[1]]=$ll;
            }
        }
        $result["linkout"]=$links;
    }


}
$etime = microtime(true);
$result["cost"] = $etime - $stime;
echo json_encode($result);


function getHtml($term,$ip,$useragent,$cookie)
{
        $cookie = "";
        $url = "https://pubmed.ncbi.nlm.nih.gov/".$term;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
        curl_setopt($ch, CURLOPT_REFERER ,"https://pubmed.ncbi.nlm.nih.gov/");
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if(!empty($ip)){
        	curl_setopt($ch, CURLOPT_INTERFACE, $ip);
        }
        if(!empty($cookie)){
        	curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        }

        $content = curl_exec($ch);
        preg_match("/location:(.+?)\r\n/i",$content,$match);
        if($match){
        	curl_setopt($ch, CURLOPT_URL, "https://pubmed.ncbi.nlm.nih.gov".trim($match[1]));
        	$content = curl_exec($ch);
		}
        curl_close($ch);
        list($header,$body) = explode("\r\n\r\n",$content);
        preg_match_all('/Set-Cookie: (.*);/isU',$header,$matches);
        $cookie = implode(";",$matches[1]);
        return array("body"=>$body,"cookie"=>$cookie);
}
?>
